home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d20 / msgq160s.arc / FIXAREAS.C < prev    next >
Text File  |  1991-10-26  |  4KB  |  217 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <dir.h>
  4. #define NUMAREAS         256
  5. #define STRLENGTH        300
  6. #define FILENAMELEN      80
  7.  
  8.  
  9. typedef
  10.   struct p_str{
  11.     char           string[STRLENGTH];
  12.     struct p_str  *next;
  13.   } p_str_type;
  14.  
  15.  
  16. FILE        *infile;
  17. FILE        *outfile;
  18. int          numarray[NUMAREAS];
  19. char        *p_strings;
  20. int          stringnum=0;
  21. p_str_type  *p_head;
  22. p_str_type  *this_p;
  23.  
  24.  
  25.  
  26. void error(char *errmsg)
  27. {
  28.   printf("\nERROR: %s\n",errmsg);
  29.   exit(1);
  30. }
  31.  
  32.  
  33. char *change_extension(char *infile,
  34.                char *outfile)
  35. {
  36.   char *p;
  37.  
  38.   strcpy(outfile, infile);
  39.   p = strrchr(outfile, '.');
  40.   if (p != NULL)
  41.     *p = '\0';
  42.   strcat(outfile, ".bak");
  43.  
  44. }
  45.  
  46.  
  47. void openfiles(char *outfilename)
  48. {
  49.   char infilename[FILENAMELEN];
  50.   struct ffblk blk;
  51.  
  52.   change_extension(outfilename,infilename);
  53.   if (findfirst(infilename, &blk, 0) == 0)
  54.     unlink(infilename);
  55.   rename(outfilename,infilename);
  56.   if ((infile = fopen(infilename,"r")) == NULL)
  57.     error("could not open input file");
  58.   if ((outfile = fopen(outfilename,"w")) == NULL)
  59.     error("could not open output file");
  60. }
  61.  
  62.  
  63. void initarray()
  64. {
  65.   int x;
  66.  
  67.   for (x=0;x<NUMAREAS;x++)
  68.     numarray[x] = 0;
  69. }
  70.  
  71.  
  72. int markarray(int num)
  73. {
  74.   numarray[num] = 1;
  75. }
  76.  
  77.  
  78. int getlowest()
  79. {
  80.   int x;
  81.  
  82.   x = 1;
  83.   while ((numarray[x] != 0) && (x < NUMAREAS))
  84.     x++;
  85.   numarray[x] = 1;
  86.   return(x);
  87. }
  88.  
  89.  
  90. void storestr(char *str)
  91. {
  92.   p_str_type  *temp_p;
  93.  
  94.   if (stringnum == 0)
  95.     {
  96.       p_head = (p_str_type *)malloc(sizeof(p_str_type));
  97.       if (p_head == NULL)
  98.     error("memory allocation error");
  99.       strcpy(p_head->string,str);
  100.       p_head->next = NULL;
  101.       this_p = p_head;
  102.       stringnum++;
  103.     }
  104.   else
  105.     {
  106.       temp_p = (p_str_type *)malloc(sizeof(p_str_type));
  107.       if (temp_p == NULL)
  108.     error("memory allocation error");
  109.       strcpy(temp_p->string,str);
  110.       temp_p->next = NULL;
  111.       this_p->next = temp_p;
  112.       this_p = this_p->next;
  113.       stringnum++;
  114.     }
  115. }
  116.  
  117.  
  118. void out_p_strings()
  119. {
  120.   int   x;
  121.   int   num;
  122.   char  numstr[STRLENGTH+5];
  123.  
  124.   fputs("\n",outfile);
  125.   this_p = p_head;
  126.   for (x=0;x<stringnum;x++)
  127.     {
  128.       num = getlowest();
  129.       sprintf(numstr,"%03d%s",num,this_p->string);
  130.       fputs(numstr,outfile);
  131.       this_p = this_p->next;
  132.     }
  133. }
  134.  
  135.  
  136. void parse()
  137. {
  138.   char  filestr[STRLENGTH+1];
  139.   char  parsestr[STRLENGTH+1];
  140.   char *savestr;
  141.   char  ch;
  142.   int   num;
  143.  
  144.   while (!(feof(infile)))
  145.     {
  146.       if (fgets(filestr,STRLENGTH,infile) == NULL)
  147.     {
  148.       if (!(feof(infile)))
  149.         error("could not read input file");
  150.     }
  151.       else
  152.     if (sscanf(filestr," %[Pp;]",parsestr) == 1)
  153.       {
  154.         if (strlen(parsestr) != 0)
  155.           {
  156.         ch = parsestr[strlen(parsestr)-1];
  157.         savestr = strchr(filestr,ch);
  158.         strcpy(savestr,&savestr[1]);
  159.         if (ch == 'P')
  160.           storestr(savestr);
  161.         else
  162.           if (ch == ';')
  163.             {
  164.               num = atoi(savestr);
  165.               markarray(num);
  166.               if (fputs(parsestr,outfile) == EOF)
  167.             error("could not write to output file");
  168.               if (fputs(savestr,outfile) == EOF)
  169.             error("could not write to output file");
  170.             }
  171.           }
  172.       }
  173.     else
  174.       {
  175.         num = atoi(&filestr[0]);
  176.         markarray(num);
  177.         if (fputs(filestr,outfile) == EOF)
  178.           error("could not write to output file");
  179.       }
  180.     }
  181. }
  182.  
  183.  
  184. void closefiles()
  185. {
  186.   if (fclose(infile) != 0)
  187.     error("could not close input file");
  188.   if (fclose(outfile) != 0)
  189.     error("could not close output file");
  190. }
  191.  
  192.  
  193. void p_copy()
  194. {
  195.   initarray();
  196.   parse();
  197.   out_p_strings();
  198. }
  199.  
  200.  
  201. void main(int   argc,
  202.       char *argv[])
  203. {
  204.   char infilename[FILENAMELEN];
  205.   char outfilename[FILENAMELEN];
  206.  
  207.  
  208.   if (argc < 2)
  209.     {
  210.       printf("  usage: fixareas areas.bbs\n");
  211.       exit(1);
  212.     }
  213.   openfiles(argv[1]);
  214.   p_copy();
  215.   closefiles();
  216.   exit(0);
  217. }